package com.panshi.controller.information;

import com.panshi.api.SortApi;
import com.panshi.po.SortPO;
import com.panshi.po.SystemUserPo;
import com.panshi.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
@RequestMapping("/classify")
public class ZXClassifyController {

    @Resource
    private SortApi sortApi;

    @GetMapping("/query")
    public String classifyPage(){

        return "information/zxclassify/zx-query";
    }

    @PostMapping("/query")
    @ResponseBody
    public PageModel<SortPO> classify(PageModel<SortPO> pm){
        pm.init();
        PageVO pageVO = new PageVO();
        pageVO.setCurrenPage(pm.getCurrentPage());
        pageVO.setSize(pm.getPageSize());

        List<SortPO> sortPO = sortApi.sortQuery(pageVO).getData();
        pm.setList(sortPO);
        log.info("sort ---------------- {}",sortPO);

        Integer count = sortApi.queryAllTotal().getData();
        pm.setCount(count);

        return pm;
    }

    @PostMapping("/query-vague/{vague}")
    @ResponseBody
    public PageModel<SortPO> queryVague(PageModel<SortPO> pm, @PathVariable String vague){
        pm.init();
        log.info("pm ------------------------{}",pm);

        SortDimQueryVO sortDimQueryVO = new SortDimQueryVO();
        sortDimQueryVO.setName(vague);
        sortDimQueryVO.setCurrenPage(pm.getCurrentPage());
        sortDimQueryVO.setSize(pm.getPageSize());

        List<SortPO> sortPOList = sortApi.dimQuery(sortDimQueryVO).getData();
        pm.setList(sortPOList);

        Integer count = sortApi.nameDimQuery(vague).getData();
        pm.setCount(count);

        return pm;
    }

    @GetMapping("/add")
    public String addPage(){

        return "information/zxclassify/classify-add";
    }

    @PostMapping("/add")
    @ResponseBody
    public PageModel add(SortAddVO addVo){

        log.info("addVo---------------------{}",addVo);

        PageModel pageModel = new PageModel();
        addVo.setCreatedate(new Date());

        Integer state = sortApi.sortAdd(addVo).getData();

        log.info("state**************************{}",state);

        if(state == 0){
            pageModel.setCode(9999);
            pageModel.setMsg("新增失败");
        }
        if(state == 1){
            pageModel.setCode(10000);
            pageModel.setMsg("新增成功");
        }

        return pageModel;
    }

    @GetMapping("/edit")
    public String editPage(@RequestParam Integer id, Model model){

        SortPO sortPO = sortApi.byIdQuery(id).getData();

        model.addAttribute("sort",sortPO);

        return "information/zxclassify/classify-edit";
    }

    @PostMapping("/edit")
    @ResponseBody
    public Map<String,Object> edit(SortUpdateVO updateVo){

        Map<String,Object> map = new HashMap<>();

        updateVo.setUpdatedate(new Date());

        log.info("资讯分类修改------------------------SortUpdateVO ：{}",updateVo);

        Integer state = sortApi.sortUpdate(updateVo).getData();

        log.info("资讯分类修改状态------------------------state ：{}",state);

        if(state == 0){
            map.put("success",false);
            map.put("message","修改失败，请重新修改");
        }else if(state == 1){
            map.put("success",true);
            map.put("message","修改成功");
        } else if (state == -1) {
            map.put("success",false);
            map.put("message","该分类名称已经存在");
        }

        return map;
    }

    @PostMapping("/del")
    @ResponseBody
    public Map<String,Object> del(SortUpdateStateVo sortUpdateStateVo){

        Map<String,Object> map = new HashMap<>();

        log.info("资讯分类删除，将状态改为禁用------------------------sortUpdateStateVo ：{}",sortUpdateStateVo);

        Integer state = sortApi.updateState(sortUpdateStateVo).getData();

        if(state == 0){
            map.put("success",false);
            map.put("message","删除失败");
        }else if(state == 1){
            map.put("success",true);
            map.put("message","删除成功");
        }
        return map;

    }

}
